<!-- 充值 -->
<template>
  <div class="turnIn">
    <x-header :left-options="{backText: ''}">充值</x-header>
    <div class="transferOutAddress">选择提现地址</div>
    <div class="transferOutBox">
      <div class="address" @click="goTo()">
        <!-- <img src="../../assets/qrCode.png" alt="" width="20" height="20" style="vertical-align: middle;"> -->
        <span style="vertical-align: middle;" v-if="!address">我的地址</span>
        <div style="color:#999" v-if="address">{{address}}</div>
        <img src="../../assets/nav.png" alt="" width="10" class="transferOutNav">
      </div>
      <div class="turnInAddress">
        <div style="padding-left:15px;margin-top:10px;">
          <div style="color:#999">充值地址</div>
         
          <div class="copyBox">
            <!-- 用于放置复制的内容 -->
            <input id="foo" value="0x65e7Fd5479beB0D11C0073259E22809E2e3BEa6e" style="border:none;outline: none;color:#999;width: 80%;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;" readonly>
            <!-- 复制按钮 -->
            <div class="copyBtn">
              <copy :show="true" :value="11" :text="'复制'" :id="'#foo'"></copy>
            </div>
          </div>
        </div>
      </div>
      <div class="address">
        <span style="color:#999">Token</span>
        <div class="token">
          <!-- <img src="../../assets/ethWallet.png" alt="" width="30" height="30" style="vertical-align: middle;"> -->
          <span style="vertical-align: middle;">{{type}}</span>
        </div>
      </div>
      <div class="transferOutMoney">
        <div style="height:10px;background: #f6f6f6"></div>
        <span class="transferOutMoneyNum">充值金额</span>
        <input placeholder="请输入充值金额" class="moneyNum" style="position: absolute;right: 15px; display: inline-block;height: 40px;line-height: 40px;border:none;text-align: right;border-style: none;outline: none;width:70%" v-model="money">
      </div>
    </div>
    <div style="padding: 0px 15px;margin-top:130px;" @click="transferOutConfim">
      <div class="transferOutConfim">
        确认充值
      </div>
    </div>

    <!-- 输入支付密码 -->
    <div class="tips" v-show="showTips">
      <div class="tips-top">
        <div style="text-align: center;height: 45px;line-height: 45px;border-bottom: 1px solid #f6f6f6;">
          <img src="../../assets/close.png" alt="" width="15" @click="closeShowTips">
          <span class="c3">请输入支付密码</span>
        </div>
        <div>
          <div class="tips-center">
            <li>
              <input type="password" v-model="pwd1" maxlength="1" @keyup="checkPwd" class="input">
            </li>
            <li>
              <input type="password" v-model="pwd2" maxlength="1" @keyup="checkPwd" class="input">
            </li>
            <li>
              <input type="password" v-model="pwd3" maxlength="1" @keyup="checkPwd" class="input">
            </li>
            <li>
              <input type="password" v-model="pwd4" maxlength="1" @keyup="checkPwd" class="input">
            </li>
            <li>
              <input type="password" v-model="pwd5" maxlength="1" @keyup="checkPwd" class="input">
            </li>
            <li>
              <input type="password" v-model="pwd6" maxlength="1" @keyup="checkPwd" class="input">
            </li>
          </div>
          <div class="forgetPassWord" @click="$router.push({path:'/main/forgetPay'})">忘记密码?</div>
        </div>
      </div>
    </div>
    <!-- 支付密码错误弹框 -->
    <div class="errTips" v-if="errTips">
      <div class="errTipsBox">
        <div class="errTips-top">
          支付密码错误,请重试!
        </div>
        <div class="errTipsBtn">
          <span class="active" @click="retry">重试</span>
          <span @click="$router.push({path:'/main/forgetPay'})">忘记密码?</span>
        </div>
        <div style="margin-left: 30px;font-size: 13px;margin-top: 5px;color: #ff1010">(说明：只能重试3次)</div>
      </div>
    </div>
    <!-- 设置支付密码 -->
    <div class="setPassWorldTips" v-if="isPassWorld">
      <div class="setPasswordText">
        <div class="setPassWorldTitle">请设置支付密码!</div>
        <div class="goPassworld" @click="$router.push({path:'/main/setPayPassword'})">立即前往</div>
      </div>
    </div>
  </div>
</template>
<script>
  import { XHeader } from 'vux'
  import Copy from '../../components/common/copy.vue'
  import http from '../../service/http.js'
  import tips from '../../service/tips.js'
  import util from '../../service/util.js'
  export default {
    data() {
      return {
        type: "",
        id: "",
        address: "",
        money: "",
        showTips: false,//显示支付密码弹框
        errTips: false,//显示支付错误弹框
        isPassWorld: false,//判断有没有支付密码
        pwd: "",
        pwd1: "",
        pwd2: "",
        pwd3: "",
        pwd4: "",
        pwd5: "",
        pwd6: "",
      }
    },
    components: {
      XHeader,
      Copy
    },
    mounted() {
      this.hbAmount = this.$route.query.amount;
      var txts = document.querySelectorAll(".input");
      for (var i = 0; i < txts.length; i++) {
        var t = txts[i];
        t.index = i;
        t.setAttribute("readonly", true);
        t.onkeyup = function () {
          this.value = this.value.replace(/^(.).*$/, '$1');
          var next = this.index + 1;
          if (next > txts.length - 1) return;
          txts[next].removeAttribute("readonly");
          txts[next].focus();
        }
      }
      txts[0].removeAttribute("readonly");
      this.type = this.$route.query.type;
      this.id = this.$route.query.id;
      this.getAddress();
    },
    methods: {
      transferOutConfim() {
        if (!this.id) {
          tips.tips("请选择地址");
          return false;
        }
        if (!this.money) {
          tips.tips("请输入金额");
          return false;
        }
        this.hasPay();
      },
      goTo() {
        this.$router.push({ path: "/main/select", query: { type: this.type, status: 1 } })
      },
      getAddress() {
        http.get("/user/address").then((res) => {
          if (res.status == 200) {
            res.data.body.forEach((v) => {
              if (v.id == this.id) {
                this.address = v.address
              }
            })
          }
        })
      },
      // 关闭支付密码输入弹框
      closeShowTips() {
        this.showTips = false;
        this.pwd1 = ""
        this.pwd2 = ""
        this.pwd3 = ""
        this.pwd4 = ""
        this.pwd5 = ""
        this.pwd6 = ""
      },
      // 重试按钮
      retry() {
        this.errTips = false;
        this.showTips = true;
        this.pwd1 = ""
        this.pwd2 = ""
        this.pwd3 = ""
        this.pwd4 = ""
        this.pwd5 = ""
        this.pwd6 = ""
      },
      //判断有无支付密码
      hasPay() {
        http.post('/user/hasPay', {
          username: window.localStorage.getItem("login.username")
        }).then((res) => {
          if (res.status == 200 && !res.data.body) {//无支付密码
            this.isPassWorld = true;
          }
          if (res.status == 200 && res.data.body) { //有支付密码(下单)
            this.showTips = true;
          }
        })
      },
      //校验支付密码
      checkPwd() {
        this.pwd = this.pwd1 + this.pwd2 + this.pwd3 + this.pwd4 + this.pwd5 + this.pwd6;
        if (this.pwd1 && this.pwd2 && this.pwd3 && this.pwd4 && this.pwd5 && this.pwd6) {
          http.post('/user/authPay', {
            payPwd: util.md5(this.pwd)
          }).then((res) => {
            if (res.data.status == 200) {
              http.put("/user/coin/charge", {
                payAmount: this.money,
                id: this.id,
                name: this.$route.query.type
              }).then((res) => {
                if (res.status == 200) {
                  // tips.tips(res.data.msg);
                  this.$router.push({ path: "/main/applicationRecharge" })
                } else {
                  tips.tips("充值失败");
                }
              }).catch((res) => {
                tips.tips(res.data.msg);
              })
            }
          })
        }
      },
    }
  }
</script>
<style lang="less">
  body {
    background: #f6f6f6;
  }

  .turnIn .vux-header {
    background: #fff;
  }

  .turnIn .vux-header .vux-header-title {
    line-height: 40px;
    text-align: center;
    font-size: 18px;
    font-weight: 400;
    color: #000;
  }

  .turnIn .vux-header .vux-header-left .left-arrow:before {
    content: "";
    position: absolute;
    width: 12px;
    height: 12px;
    border: 1px solid #000;
    border-width: 1px 0 0 1px;
    -webkit-transform: rotate(315deg);
    transform: rotate(315deg);
    top: 8px;
    left: 7px;
  }

  .turnIn .transferOutAddress {
    height: 30px;
    line-height: 30px;
    padding-left: 15px;
    color: #999;
  }

  .turnIn .transferOutBox {
    background: #fff;
  }

  .turnIn .transferOutBox .address {
    position: relative;
    height: 50px;
    line-height: 50px;
    border-bottom: 1px solid #f6f6f6;
    padding: 0px 15px;
  }

  .turnIn .transferOutBox .address .transferOutNav {
    position: absolute;
    right: 15px;
    top: 15px;
  }

  .turnIn .transferOutBox .address .token {
    position: absolute;
    right: 15px;
    top: 0px;
  }

  .turnIn .transferOutMoneyNum {
    height: 40px;
    line-height: 40px;
    display: inline-block;
    padding: 0 15px;
  }

  .transferOutMoney {
    position: relative;
  }

  .transferOutConfim {
    height: 40px;
    text-align: center;
    line-height: 40px;
    border-radius: 25px;
    background: -webkit-gradient(linear,left top,right top,from(#fedd32),to(#fe9e09));    
    color: #fff;
  }

  .turnInAddress {
    height: 70px;
    border: 1px solid #f6f6f6;
  }

  .turnIn .turnInAddress .copyBox {
    position: relative;
  }

  .turnIn .turnInAddress .copyBtn {
    position: absolute;
    right: 15px;
    display: inline-block;
    width: 50px;
    text-align: center;
    height: 20px;
    line-height: 20px;
    color: #fff;
    background: linear-gradient(left, #fedb30, #fe9f0a);
    background-image: linear-gradient(-135deg, #fedb30 0%, #fe9f0a 100%);
    font-size: 13px;
  }

  .tips {
    position: fixed;
    background: rgba(0, 0, 0, 0.3);
    top: 0px;
    bottom: 0px;
    left: 0px;
    right: 0px;
    z-index: 999;
    .tips-top {
      position: absolute;
      top: 40%;
      height: 60%;
      background: #fff;
      width: 100%;
      border-bottom: 1px solid #f6f6f6;
      img {
        position: absolute;
        left: 15px;
        top: 15px;
      }
    }
  }

  .tips-center {
    padding-left: 10%;
    margin-top: 45px;
  }

  .tips-center li {
    float: left;
    width: 15%;
    list-style: none;
    height: 45px;
    line-height: 45px;
    border: 1px solid #ccc;
    border-right: none;
    overflow: hidden;
    text-align: center;
    input {
      border: none;
      outline: none;
      height: 45px;
      line-height: 45px;
      width: 100%;
      display: block;
      text-align: center;
    }
  }

  .tips-center li:last-child {
    border-right: 1px solid #ccc;
  }

  .forgetPassWord {
    position: absolute;
    right: 28px;
    top: 150px;
    color: #fea620;
  }

  .errTips {
    position: fixed;
    top: 0px;
    bottom: 0px;
    left: 0px;
    right: 0px;
    background: rgba(0, 0, 0, 0.3);
    z-index: 999;
  }

  .errTipsBox {
    position: absolute;
    top: 40%;
    width: 90%;
    height: 130px;
    background: #fff;
    margin-left: 5%;
    border-radius: 15px;
    .errTips-top {
      height: 60px;
      line-height: 60px;
      text-align: center;
      border-bottom: 1px solid #efefef;
    }
    .errTipsBtn {
      span {
        display: inline-block;
        width: 48%;
        text-align: center;
        margin-top: 15px;
        color: #fea620;
      }
      .active {
        border-right: 1px solid #efefef;
      }
    }
  }

  .setPassWorldTips {
    position: fixed;
    top: 0px;
    left: 0px;
    bottom: 0px;
    right: 0px;
    background: rgba(0, 0, 0, 0.3);
    z-index: 999;
  }

  .setPasswordText {
    position: absolute;
    top: 40%;
    height: 130px;
    background: #fff;
    width: 90%;
    margin-left: 5%;
    border-radius: 15px;
    .setPassWorldTitle {
      height: 60px;
      line-height: 60px;
      text-align: center;
      border-bottom: 1px solid #efefef;
      color: #000;
    }
    .goPassworld {
      height: 70px;
      line-height: 65px;
      color: #fea620;
      text-align: center;
    }
  }
</style>